<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>商家注册</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/js/layui/css/layui.css" media="all">


</head>
<body>
<br>
<form class="layui-form" action="">
    <%--店铺名称--%>
    <div class="layui-form-item">
        <label class="layui-form-label">店铺名称<span style="color: red; font-size: larger"> *</span></label>
        <div class="layui-input-block">
            <input type="text" id="shopName" style="width: 350px" name="username" value="" lay-verify="title" lay-reqtext="店铺名称是必填项，岂能为空？" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <%--手机号--%>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">手机号<span style="color: red; font-size: larger"> *</span></label>
            <div class="layui-input-inline">
                <input type="tel" id="accTelephone" name="liaisonsTelephone" lay-verify="required|phone" lay-reqtext="手机号是必填项，岂能为空？" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <%--账户密码--%>
    <div class="layui-form-item">
        <label class="layui-form-label">账户密码<span style="color: red; font-size: larger"> *</span></label>
        <div class="layui-input-block">
            <input type="password" id="accPwd" lay-verify="required" style="width: 350px" name="password" lay-reqtext="账户密码是必填项，岂能为空？" placeholder="请输入密码" autocomplete="off" class="layui-input">
        </div>
    </div>
    <%--表单提交--%>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script src="${pageContext.request.contextPath}/assets/js/core/jquery.min.js"></script>
<script src="${pageContext.request.contextPath}/js/layui/layui.js"></script>


<script>
    var accId = ${param.accId}; //获得 首页面传递过来的参数值
    var truePwd = "";

    var param = {
        accId:accId
    }
    var url = "/accountInfo/selectAccountByAccId";
    $.get(url,param,function (data){
        if(data.code == 1001){
            truePwd = data.account.accPwd;
        }
    })

    layui.use(['form', 'layedit', 'laydate'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate;

        form.render();

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //自定义表单验证规则 （input输入框中的属性 lay-verify 的值与下面对应）
        // required 内置的校验 输入框内容不能为空
        //title 指标题验证，pass 指密码验证，可以自定义正则表达式，confirmPass 指两次密码相同验证
        form.verify({
            title: function(value){
                if(value.length < 3){
                    return '店铺至少得3个字符啊';
                }
            }
            ,pass: [            //定义密码的正则表达式
                /^[\S]{6,12}$/
                ,'密码必须6到12位，且不能出现空格'
            ]
            ,content: function(value){
                layedit.sync(editIndex);
            }
            ,confirmPass:function(value){
                if($('input[name=password]').val() !== value)
                    return '两次密码输入不一致！';
            }
        });

        //监听表单提交
        form.on('submit(demo1)', function(data){
            layer.confirm('确定提交吗？', function(index){ //弹出确定提交弹窗
                var accPwd = $("#accPwd").val();
                var param = {
                  accId:accId,
                    shopName: $("#shopName").val(),
                    accPwd: accPwd,
                    accTelephone: $("#accTelephone").val(),
                    _method:'put'
                }
                var url = "/business/registerBusiness";
                if(truePwd == accPwd){
                    /*发送商家注册请求*/
                    $.post(url,param,function (data){
                        if(data.code == 1001){
                            //    layer.msg(data.msg);
                            layer.closeAll();       //关闭确认提交的弹出
                            layer.open({
                                type: 1
                                ,content: '<div style="padding: 20px 100px;">'+ data.msg +'</div>'
                                ,btn: '确定'
                                ,btnAlign: 'c' //按钮居中
                                ,shade: 0 //不显示遮罩
                                ,yes: function(){
                                    //    layer.closeAll();
                                    top.location='../home/frontend_home.jsp';  //点击确定后跳转首页面
                                }
                            });

                        }else if(data.code == 1002){
                            layer.msg(data.msg);
                        }
                    })
                }else {
                    layer.msg("账户密码错误");
                    $("#accPwd").val("");
                }


            });

            return false;
        });

        //表单取值
        layui.$('#LAY-component-form-getval').on('click', function(){
            var data = form.val('example');
            alert(JSON.stringify(data));
        });

    });
</script>
</body>
</html>
